<template>
	<div>
		<!-- 发送邮件提示窗 -->
		<uni-popup ref="sendEmailDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" confirmText="确认" title="发送邮件"
				@confirm="sendEmailDialogConfirm" :before-close="true" @close="closeDialog('sendEmailDialog')">
				<scroll-view scroll-top="0" scroll-y="true" style="height: 60px;">
					<uni-forms ref="form" :modelValue="formData" :rules="rules" labelWidth="75px">
						<uni-forms-item label="电子邮箱" name="email" required>
							<uni-easyinput type="text" v-model="formData.email" placeholder="请输入电子邮箱" />
						</uni-forms-item>
					</uni-forms>
				</scroll-view>
			</uni-popup-dialog>
		</uni-popup>
	</div>
</template>

<script>
	import{
		sendEmail
	} from '@/api/system/invoice.js'
	export default {
		data() {
			return {
				//开票
				formData:{
					email: '',
					id:''
				},
				// 校验规则->开票
				rules: {
					email: {
						rules: [{
							required: true,
							errorMessage: '请输入邮箱',
						},
						{
						    pattern: '^(\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$)',
						    errorMessage: "请输入正确的邮箱"
						}
						]
					},
				},
			}
		},
		methods: {
			//关闭弹窗
			closeDialog(name){
				this.$refs[name].close()
			},
			//发送邮件
			sendEmailDialog(row){
				this.formData.email=row.email
				this.formData.id=row.id  //开票id
				this.$refs.sendEmailDialog.open()
			},
			//确认发送
			sendEmailDialogConfirm(){
				this.$refs.form.validate().then(res => {
					uni.showLoading({
						title: '正在发送...',
					})
					//发送完成
					sendEmail(this.formData.id,this.formData.email).then(res => {
						uni.hideLoading()
						if (res.code == '200') {
							uni.showToast({
								title: '发送邮件成功',
								icon: 'none'
							})
							this.$refs.sendEmailDialog.close()
							//this.$forceUpdate(); //重新加载数据
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}	
					})
				}).catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				})
				
			},
			
		},
		
	}
</script>

<style scoped>

</style>